package cn.bluech.mvvmkit.binding.adapter

import android.util.Log
import android.view.View
import androidx.databinding.BindingAdapter

/**
 * BindingAdapter这个注解用于支持自定义属性或者修改原有属性。
 * 注解的值可以是已有的xml属性值，如android:text。也可以是自定义属性。
 *
 * 绑定适配器方法的参数: 第一个参数确定与属性关联的View类型。
 *                   第二个及以后的参数确定给定属性的绑定表达式中接受的参数类型(可以接收多个属性)。
 * BindingMethod只能绑定目标类的已有方法。
 * BindingAdapter绑定系统中(ViewBindingAdapter)已经绑定的属性时会覆盖系统的.
 * @see androidx.databinding.adapters.ViewBindingAdapter
 *
 *
 */

//@BindingMethods(
//    BindingMethod(
//        type = View::class,
//        attribute = "android:onClick",
//        method = "setOnClickListener"
//    )
//)
object ViewAdapter {

    @JvmStatic
//    @BindingAdapter("isGone")
    @BindingAdapter(value = ["isVisible"], requireAll = false)
    fun isGone(view: View, isVisible: Boolean) {
        Log.e("ViewAdapter","isVisible=$isVisible")
        view.visibility = if (isVisible) {
            View.VISIBLE
        } else {
            View.GONE
        }
    }

    /*
    @JvmStatic
//    @BindingAdapter(value = ["android:onClick","throttle"], requireAll = false)
    fun onClick(view: View,callback: (View)->Unit , throttle: Long) {
//    @BindingAdapter(value = ["android:onClick"], requireAll = false)
//    fun onClick(view: View,callback: (View)->Unit) {
        Log.e("ViewAdapter","onClick")

        var observable = Observable.create<View>{emitter ->
            view.setOnClickListener { emitter.onNext(view) }
        }
            .subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())

//        if(throttle>0){
//            observable = observable.throttleFirst(throttle,TimeUnit.MILLISECONDS)
//        }
        observable = observable.throttleFirst(1000,TimeUnit.MILLISECONDS)
        val onNext = Consumer<View>{
            callback.invoke(it)
        }
        val onError = object : Consumer<Throwable>{
            override fun accept(t:Throwable){
                t.printStackTrace()
                // 重新注册
                val disposable = observable.subscribe(onNext,this)
            }
        }

        val disposable = observable.subscribe (onNext,onError)
    }
    */

    @JvmStatic
    @BindingAdapter(value = ["android:onClick", "app:clickThrottle"])
//    @BindingAdapter(value = ["android:onClick", "clickThrottle"])
    fun setOnClick(view: View, listener: View.OnClickListener?, throttle: Long) {
        Log.e("ViewAdapter","setOnClick clickThrottle=$throttle")
        if(null==listener) {
            view.setOnClickListener(null)
            return
        }
        if (throttle>0){
            view.setOnClickListener(OnClickThrottleListener(throttle,listener))
        }else{
            view.setOnClickListener(listener)
        }
        view.isClickable = true
    }

    /**
     * 防重复点击
     */
    private class OnClickThrottleListener(val throttle: Long,val listener: View.OnClickListener)
        : View.OnClickListener{
        var lastClick = 0L

        override fun onClick(view: View?) {
            if (lastClick+throttle>=System.currentTimeMillis())
                return
            lastClick = System.currentTimeMillis()
            listener.onClick(view)
        }
    }
}